package com.yanqu.road.dao.impl.config;


import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MallDaoImpl extends TempDao {

    public Map<Integer, MallInfo> getMallInfo() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_mall";
        Map<Integer, MallInfo> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                MallInfo data = new MallInfo();
                data.setMallId(rs.getInt("id"));
                data.setRechargeId(rs.getInt("rechargeId"));
                data.setMallName(rs.getString("name"));
                data.setShopType(rs.getInt("shopType"));
                data.setCategory(rs.getInt("category"));
                data.setReward(rs.getString("reward"));
                data.setRewardProp(PropertyHelper.parseStringToProperty(data.getReward()));
                data.setPrice(rs.getString("price"));
                data.setPriceAdd(rs.getInt("priceAdd"));
                data.setFirstDouble(rs.getBoolean("isDouble"));
                data.setLimitBuyType(rs.getInt("buyLimitType"));
                data.setLimitBuyCount(rs.getInt("buyLimit"));
                data.setSort(rs.getInt("sort"));
                data.setReturnIngots(rs.getInt("returnIngots"));
                data.setParam(rs.getString("param"));
                data.setPointSign(rs.getString("pointSign"));
                data.setOldPrice(rs.getString("oldPrice"));
                data.setVipLimit(rs.getInt("vipLimit"));
                data.setBuyLimitParam(rs.getString("buyLimitParam"));
                List<Integer> systemList = new ArrayList<>();
                String systemLimitStr = rs.getString("systemLimit");
                if (!StringUtils.isNullOrEmpty(systemLimitStr) && !systemLimitStr.equals("0")) {
                    systemList = StringUtils.stringToIntegerList(systemLimitStr, ";");
                }
                data.setSystemLimit(systemList);
                dataMap.put(data.getMallId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}